package com.jd.org.controller;

import com.jd.basic.util.AjaxResult;
import com.jd.basic.util.PageList;
import com.jd.org.domain.Employee;
import com.jd.org.service.EmployeeService;
import com.jd.org.query.EmployeeQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "员工接口类",description = "员工接口类")
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @ApiOperation(value = "查询单个数据")
    @GetMapping("/{id}")
    public Employee findOne(@PathVariable("id") Long id) {
        return employeeService.loadById(id);
    }

    @ApiOperation(value = "查询全部数据")
    @GetMapping
    public List<Employee> findAll() {
        return employeeService.loadAll();
    }

    @ApiOperation(value = "删除数据")
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id")Long id) {
        try {
            employeeService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new AjaxResult(false,"删除失败了");
    }

    @ApiOperation(value = "添加或修改数据")
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Employee employee) {
        try {
            if (employee.getId() == null) {
                employeeService.save(employee);
            }else {
                employeeService.update(employee);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    @ApiOperation(value = "高级查询")
    @PostMapping
    public PageList<Employee> find(@RequestBody EmployeeQuery employeeQuery) {
        return employeeService.queryPage(employeeQuery);
    }

    @ApiOperation(value = "批量删除数据")
    @PatchMapping
    public AjaxResult patchRemove(@RequestBody List<Long> ids) {
        try {
            // 批量删除
            employeeService.patchRemove(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new AjaxResult(false,"批量除失败");
    }

    // 根据店铺id查询店铺人员 - 店长，店员
    @GetMapping("/list/{id}")
    public List<Employee> findByShopId(@PathVariable("id") Long shopId) {
        return employeeService.findByShopId(shopId);
    }


}
